package com.yaorange.web.controller.nongjt;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.context.properties.bind.DefaultValue;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yaorange.common.annotation.Log;
import com.yaorange.common.core.controller.BaseController;
import com.yaorange.common.core.domain.AjaxResult;
import com.yaorange.common.enums.BusinessType;
import com.yaorange.nongjt.domain.NjtCertificateCount;
import com.yaorange.nongjt.service.INjtCertificateCountService;
import com.yaorange.common.utils.poi.ExcelUtil;
import com.yaorange.common.core.page.TableDataInfo;

/**
 * 合格证统计数据Controller
 *
 * @author juheng
 * @date 2025-02-28
 */
@RestController
@RequestMapping("/nongjt/certificate_count")
public class NjtCertificateCountController extends BaseController
{
    @Autowired
    private INjtCertificateCountService njtCertificateCountService;

    /**
     * 查询合格证统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:list')")
    @GetMapping("/list")
    public TableDataInfo list(NjtCertificateCount njtCertificateCount)
    {
        startPage();
        List<NjtCertificateCount> list = njtCertificateCountService.selectNjtCertificateCountList(njtCertificateCount);
        return getDataTable(list);
    }

    /**
     * 导出合格证统计数据列表
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:export')")
    @Log(title = "合格证统计数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtCertificateCount njtCertificateCount)
    {
        List<NjtCertificateCount> list = njtCertificateCountService.selectNjtCertificateCountList(njtCertificateCount);
        ExcelUtil<NjtCertificateCount> util = new ExcelUtil<NjtCertificateCount>(NjtCertificateCount.class);
        util.exportExcel(response, list, "合格证统计数据数据");
    }

    /**
     * 获取合格证统计数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(njtCertificateCountService.selectNjtCertificateCountById(id));
    }

    /**
     * 新增合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:add')")
    @Log(title = "合格证统计数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NjtCertificateCount njtCertificateCount)
    {
        return toAjax(njtCertificateCountService.insertNjtCertificateCount(njtCertificateCount));
    }

    /**
     * 修改合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:edit')")
    @Log(title = "合格证统计数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NjtCertificateCount njtCertificateCount)
    {
        return toAjax(njtCertificateCountService.updateNjtCertificateCount(njtCertificateCount));
    }

    /**
     * 删除合格证统计数据
     */
    @PreAuthorize("@ss.hasPermi('nongjt:certificate_count:remove')")
    @Log(title = "合格证统计数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(njtCertificateCountService.deleteNjtCertificateCountByIds(ids));
    }

    /**
     * 获取年份列表
     * @return
     */
    @GetMapping("/yearList")
    public AjaxResult yearList(){
        List<String> list = njtCertificateCountService.getYearList();
        return AjaxResult.success(list);
    }

    /**
     * 获取柱状图的数据
     * @param year
     * @param unitId
     * @return
     */
    @GetMapping("/getPrintCountList")
    public AjaxResult getPrintCountList(@RequestParam("year") String year, @RequestParam("unitId") @DefaultValue("0") Long unitId){
        List<Map<String, Object>> list = njtCertificateCountService.selectPrintCountList(year, unitId);
        List<Object> mounts = list.stream().map(obj -> obj.get("mounts")).collect(Collectors.toList());
        List<Object> counts = list.stream().map(obj -> obj.get("counts")).collect(Collectors.toList());

        Map<String, Object> map = new HashMap<>();
        map.put("mounts", mounts);
        map.put("counts", counts);
        return AjaxResult.success(map);
    }

    /**
     * 街道/镇合格证打印数量统计
     * @param year
     * @param unitId
     * @return
     */
    @GetMapping("/streetStatisticsByArea")
    public AjaxResult streetStatisticsByArea(@RequestParam("yearStr") String year, @RequestParam("unitId") Long unitId){
        List<Map<String, Object>> list = njtCertificateCountService.streetStatisticsByArea(year, unitId);
        return AjaxResult.success(list);
    }
}
